<template>
  <el-dialog v-model="dialogVisible" :title="title" width="500" :before-close="handleClose">
    <el-form :model="form" :rules="rules" ref="formRef">
      <el-form-item label="学号" :label-width="formLabelWidth">
        <el-input v-model="form.id" autocomplete="off" />
      </el-form-item>
      <el-form-item label="姓名" :label-width="formLabelWidth" prop="name">
        <el-input v-model="form.name" autocomplete="off" />
      </el-form-item>
      <el-form-item label="性别" :label-width="formLabelWidth" prop="sex">
        <el-select v-model="form.sex" placeholder="请选择">
          <el-option label="男" value="1" />
          <el-option label="女" value="0" />
        </el-select>
      </el-form-item>
      <el-form-item label="班级" :label-width="formLabelWidth" prop="class">
        <el-select v-model="form.class" multiple placeholder="请选择" style="width: 100%">
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          />
        </el-select>
      </el-form-item>
      <el-form-item label="年龄" :label-width="formLabelWidth" prop="age">
        <el-input-number v-model="form.age" :min="0" :max="100" />
      </el-form-item>
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button @click="handleClose">取消</el-button>
        <el-button type="primary" @click="save(formRef)"> 确认 </el-button>
      </div>
    </template>
  </el-dialog>
</template>
<script lang="ts" setup>
import { ref, reactive } from 'vue';
import { ElMessageBox } from 'element-plus';
import { FormInstance, FormRules } from 'element-plus';

interface RuleForm {
  name: string;
  sex: string;
  age: number;
  class: Array<string>;
  id: string;
  desc: string;
}

const rules = reactive<FormRules<RuleForm>>({
  id: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  name: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  sex: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  age: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
  class: [{ required: true, message: '必填项请输入内容！', trigger: 'blur' }],
});
const props = defineProps({
  visiable: Boolean,
  action: String,
});
const emit1 = defineEmits(['updateVisiable']);
const formLabelWidth = '100px';
const formRef = ref();
const titleTxt = props.action === 'add' ? '添加学生信息' : '编辑学生信息';
const title = ref(titleTxt);
const dialogVisible = ref(props.visiable);
const form = reactive({
  name: '',
  sex: '',
  age: '',
  class: [],
  id: '',
  desc: '',
});

const options = ref([
  { value: '1-1', label: '高一（1）班' },
  { value: '1-2', label: '高一（2）班' },
  { value: '1-3', label: '高一（3）班' },
  { value: '2-1', label: '高二（1）班' },
  { value: '2-2', label: '高二（2）班' },
]);
const handleClose = (done: () => void) => {
  emit1('updateVisiable', false);
};

const save = async (formEl: FormInstance | undefined) => {
  if (!formEl) return;
  await formEl.validate((valid, fields) => {
    if (valid) {
      console.log('submit!');
      if (props.action === 'add') {
        ElMessageBox.alert('新增成功', '提示', { type: 'success' });
      } else {
        ElMessageBox.alert('保存成功', '提示', { type: 'success' });
      }

      emit1('updateVisiable', false);
    } else {
      console.log('error submit!', fields);
    }
  });
};
</script>
